//
//  YWSaveButtonCell.m
//  maintain
//
//  Created by 烟雨云渊 on 16/8/11.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWSaveButtonCell.h"
#import "Masonry.h"
#import "YWMacro.h"
#import "UIColor+CustomColors.h"

@interface YWSaveButtonCell()
@property (nonatomic, strong) UILabel * remindLabel;
@property (nonatomic, strong) UIButton * saveButton;
@end

@implementation YWSaveButtonCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpSubviews];
    }
    
    return self;
}

- (void)setIsSave:(BOOL)isSave{
    _isSave = isSave;
    if (!isSave) {
        [self.saveButton setTitle:@"保存" forState:UIControlStateNormal];
        self.remindLabel.text = @"填写完毕请先保存";
        self.saveButton.backgroundColor = [UIColor customGreenColor];
        self.saveButton.tag = 2001;
    }else{
        [self.saveButton setTitle:@"撤销" forState:UIControlStateNormal];
        self.remindLabel.text = @"点击按钮取消保存";
        self.saveButton.backgroundColor = [UIColor customRedColor];
        self.saveButton.tag = 2002;
    }
}

- (void)saveButtonAction:(UIButton *)button{
    if (self.saveButton.tag == 2001) {
        if ([self.delegate respondsToSelector:@selector(clickSaveButtonEvent:)]) {
            [self.delegate clickSaveButtonEvent:button];
        }
    }else if (self.saveButton.tag == 2002){
        if ([self.delegate respondsToSelector:@selector(clickSaveButtonEvent:)]) {
            [self.delegate clickCancelButtonEvent:button];
        }
    }
    
}

- (void)setUpSubviews{
    
    [self.saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(5);
        make.right.mas_equalTo(self.contentView).offset(-10);
        make.bottom.mas_equalTo(self.contentView).offset(-5);
        make.width.mas_equalTo(@50);
        make.height.mas_equalTo(34);
    }];
    [self.remindLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.saveButton.mas_centerY);
        make.right.mas_equalTo(self.saveButton.mas_left).offset(-5);
        make.bottom.mas_equalTo(self.contentView).offset(-5);
    }];
    
}

- (UILabel *)remindLabel{
    if (!_remindLabel) {
        UILabel * label = [[UILabel alloc] init];
        label.textColor = [UIColor customTextGrayColor];
        label.font = kContentFont;
        label.text = @"填写完毕请先保存";
        [self.contentView addSubview:label];
        _remindLabel = label;
    }
    return _remindLabel;
}

- (UIButton *)saveButton{
    if (!_saveButton) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setTitle:@"保存" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(saveButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        button.clipsToBounds = YES;
        button.layer.cornerRadius = 5.0;
        button.backgroundColor = [UIColor customGreenColor];
        [self.contentView addSubview:button];
        _saveButton = button;
    }
    return _saveButton;
}
@end
